#include <avr/io.h>
#include "Cusart.h"
void USART_Init( unsigned int baud )
{
	/* Set baud rate */
	UBRRH = (unsigned char)(baud>>8);
	UBRRL = (unsigned char)baud;
	/* Enable receiver and transmitter */
	UCSRB = (1<<RXEN)|(1<<TXEN);
	/* Set frame format: 8data, 2stop bit */
	UCSRC = 0b00001110;
}




void USART_Transmit( unsigned char data )
{
	/* Wait for empty transmit buffer */
	while ( !( UCSRA & (1<<UDRE)) );


	/* Put data into buffer, sends the data */
	UDR = data;
}




unsigned char USART_Receive( void )
{
	/* Wait for data to be received */
	while ( !(UCSRA & (1<<RXC)) )
	{
		//PORTB = PORTB|2;
	}

	/* Get and return received data from buffer */
	return UDR;
}

void USART_Flush( void )
{
	unsigned char dummy;
	while ( UCSRA & (1<<RXC) ) dummy = UDR;
}

void sendString_USART(unsigned char *c)
{
	
	unsigned char i=0;
	while(c[i])
	{
		USART_Transmit(c[i++]);
	}
}

void printBinary(unsigned char c)
{
	unsigned char i;
	for(i=0;i<8;i++)
	{
		
		if(i == 4)
		{
			USART_Transmit(' ');
		}
		if(0x80 & c)
		{
			USART_Transmit('1');
		}
		else
		{
			USART_Transmit('0');
		}

		c= c<<1;

	}
	
}

unsigned char convertToHex(unsigned char c)
{
	unsigned char ret;

	if(c<10)
	{
		ret = c+'0';
	}
	else
	{
		ret = c - 10 +'A';
	}	


	return ret;
}
